$(document).ready(function() {
	var lastsel = -1;
	var elementEdit = 'faculty';
	var elementParent = '#'+elementEdit;
	var element = elementParent + 'import';
	var urlAction = '/'+elementEdit;
	jQuery(element).jqGrid({
		url : '',
		datatype : "json",
		height : "auto",
		width : "auto",
		colNames : [ 'Mã Khoa', 'Tên Khoa', 'Website', 'Điện Thoại' ],
		colModel : [ {
			name : 'name',
			index : 'name',
			width : 65,
			editable : true,
			editoptions : {
				readonly : true
			},
			editrules : {
				required : true
			}
		}, {
			name : 'facultyName',
			index : 'facultyName',
			width : 200,
			editable : true,
			editrules : {
				required : true
			}
		}, {
			name : 'facultyWebsite',
			index : 'facultyWebsite',
			width : 150,
			editable : true,
			editrules : {
				required : false,
				url : true
			}
		}, {
			name : 'facultyPhone',
			index : 'facultyPhone',
			width : 100,
			editable : true,
			editrules : {
				required : false,
				integer : true
			}
		} ],
		ignoreCase : true,
		rowNum : 20,
		rowList : [ 20, 30, 50, 100000000],
		loadonce : true,
		rownumbers : true,
		rownumWidth : 40,
		multiselect : true,
		gridview : true,
		pager : element+'pager',
		sortname : 'name',
		viewrecords : true,
		onSelectRow : function(id) {
			$(element).jqGrid('saveRow', lastsel, false, 'clientArray');
			if (id && id !== lastsel) {
				$(element).jqGrid('restoreRow', lastsel);
				lastsel = id;
			}
			$(element).jqGrid('editRow', id, false);
		},
		loadComplete : function() {
			$("option[value=100000000]").text('All');
		},
		sortorder : "asc",
		editurl : urlAction,
		caption : "Xem trước dữ liệu import",
	});
	jQuery(element).jqGrid('navGrid', element+'pager', {
		add : false,
		edit : false,
		view : false,
		excel : true
	},
	// edit option
	{
		beforeShowForm : function(formid) {
			$('#name').attr('readonly', 'readonly');
		},
		checkOnSubmit : true,
		closeAfterAdd : true,
		closeAfterEdit : true
	},
	// add option
	{
		beforeShowForm : function(formid) {
			$('#name').removeAttr('readonly');
		},
		closeAfterAdd : true,
		closeAfterEdit : true
	},
	//delete option
	{	
		beforeSubmit : function(postdata, formid) {
			$(element).jqGrid('setGridParam', { editurl : 'virtual'});
			return [true];
		},
		afterSubmit: function (response, postdata) {
			$(element).jqGrid('setGridParam', { editurl : urlAction});
			return [true];
		}
	},
	//search option
	{
		
	});
	jQuery(element).jqGrid('gridResize', {
		minWidth : 400,
		maxWidth : 1000,
		minHeight : 100,
		maxHeight : 500
	});
	jQuery(element).jqGrid('filterToolbar', {
		stringResult : true,
		searchOnEnter : false
	});
	//browser file
	jQuery(element).jqGrid('navButtonAdd', element+'pager', {
		id : 'pager_import',
		caption : '',
		title : 'Import File Excel',
		onClickButton : function(e) {
			uploadimport(elementParent);
		},
		buttonicon : 'ui-icon-folder-open'
	});
	jQuery(element).jqGrid('navButtonAdd', element+'pager', {
		id : 'pager_saveimport',
		caption : '',
		title : 'Lưu tất cả',
		onClickButton : function(e) {
			$("#load_" + elementEdit + "import").show();
			setTimeout(function() {
				var ids = $(element).jqGrid('getDataIDs');
				for ( var i = 0; i < ids.length; i++) {
					$(element).jqGrid('editRow', ids[i], false);
					$(element).jqGrid('saveRow', ids[i], false);
				}
				$("#load_" + elementEdit + "import").hide();
				var $dialog = $('<div></div>').html('Import Thành Công!').dialog({
					autoOpen : true,
					height : 150,
					width : 200,
					modal : true,
					title : 'Chú ý',
					buttons : {
						OK : function() {
							$dialog.dialog('close');
							$(elementParent).jqGrid('setGridParam', {
								datatype : 'json'
							}).trigger('reloadGrid', [ {
								page : 1
							} ]);
							$(elementParent+'popup').dialog('close');
						}
					}
				});
		    }, 100);	
		},
		buttonicon : 'ui-icon-disk'
	});
});
